package com.parse.core;

import java.util.ArrayList;
import java.util.List;

import org.com.db.api.GetDataComponet;
import org.com.db.api.ProduceComponet;
import org.com.db.core.GetDataComponetImpl;
import org.com.db.core.ProduceComponetImpl;
import org.com.db.domain.ProduceType;

import android.app.Activity;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.api.OrderService;
import com.parse.api.Padelegate;
import com.parse.api.PaserDBI;
import com.parse.bean.OrderDTO;
import com.parse.bean.ProduceResultDTO;
import com.parse.domain.ItemInfo;
import com.parse.domain.OrderInfo;
import com.parse.starter.ParseApplication;

public class OrderServiceImpl implements OrderService {

	private transient Padelegate delegate;
	private transient ProduceComponet produceComponet;
	private transient GetDataComponet getDataComponet;

	@Override
	public void saveTempOrder(OrderDTO dto, Activity activity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSendOrder(final OrderDTO dto, final Activity activity) {

		final List<ItemInfo> sonObjs = new ArrayList<ItemInfo>();

		
		
		for (String key : dto.getCarOfPurchasing().keySet()) {
			final ItemInfo itemInfo = new ItemInfo();
			itemInfo.setProduceId(key);
			itemInfo.setProduceNo(dto.getCarOfPurchasing().get(key));

			sonObjs.add(itemInfo);
		}

		final OrderInfo pareObj = new OrderInfo();
		pareObj.setUserName(dto.getUserName());
		pareObj.setUserPhone(dto.getUserPhone());
		pareObj.setUserAdress(dto.getUserAdress());
		pareObj.setCardId(dto.getCardId());
		pareObj.setMoney(dto.getMoney());
		pareObj.setStatus(dto.getStatus());
		
		PaserDBI paserDBI = PaserFactory.getDB();
		paserDBI.inserOne2Many(pareObj, sonObjs, activity);

//		activity.setResult(Activity.RESULT_OK);
//		activity.finish();

	}

	@Override
	public void queryProduces(final OrderDTO dto, final Activity activity) {

		this.delegate = (Padelegate) activity;
		// Create query for objects of type "Post"
		ParseQuery<ParseObject> query = ParseQuery.getQuery("Produce");

		// Restrict to cases where the author is the current user.
		// query.whereEqualTo("author", ParseUser.getCurrentUser());

		// Run the query
		query.findInBackground(new FindCallback<ParseObject>() {

			@Override
			public void done(List<ParseObject> postList, ParseException e) {

				if (e == null) {
					// If there are results, update the list of posts
					// and notify the adapter
					List<ProduceResultDTO> infos = new ArrayList<ProduceResultDTO>();

					for (ParseObject object : postList) {
						ProduceResultDTO info = new ProduceResultDTO();

						info.setDescribe(object.getString("describe"));
						info.setObjectId(object.getObjectId());
						info.setName(object.getString("name"));
						info.setUrl(object.getParseFile("pic").getUrl());
						info.setPrice(object.getString("price"));
						infos.add(info);

					}
					dto.getProduceInfos().clear();
					dto.getProduceInfos().addAll(infos);

					produceComponet = new ProduceComponetImpl(activity);
					produceComponet.deleteThenInsert(getProduceResuls(infos));

					delegate.putMessage(OrderServiceImpl.class.getName(), null,
							null);

				} else {
				}
			}
		});

	}

	private List<ProduceType> getProduceResuls(List<ProduceResultDTO> results) {
		List<ProduceType> types = new ArrayList<ProduceType>();
		for (ProduceResultDTO result : results) {
			ProduceType type = new ProduceType();

			type.setDescribe(result.getDescribe());
			type.setName(result.getName());
			type.setObjectId(result.getObjectId());
			type.setPrice(result.getPrice());
			type.setPic(result.getUrl());
			types.add(type);
		}
		return types;
	}
}
